package com.Policing.Input;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import androidx.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.*;

import com.Policing.BaseActivity;
import com.Policing.CanniBalize.Shift_site;
import com.Policing.Ip_set.Ip_Set;
import com.Policing.Okhttps.HttpOkHttp;
import com.Policing.R;
import com.Policing.Since_case.Since_case;
import com.bigkoo.alertview.OnDismissListener;
import com.codbking.widget.DatePickDialog;
import com.codbking.widget.bean.DateType;

import sakura.bottommenulibrary.bottompopfragmentmenu.BottomMenuFragment;
import sakura.bottommenulibrary.bottompopfragmentmenu.MenuItem;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

public class Rfid_Input extends BaseActivity implements OnDismissListener {
    private ImageView fh;//返回
    private String url="http://"+ Ip_Set.IP+"/api/assetEntry";
    private String id="",assetNumber="",assetId="",place="",istag="";
    private TextView dates,isdepre,site_tv;
    private EditText assetNumbers,num,remark,salvage,year;
    private LinearLayout salvage_lv,year_lv,num_lv;
    private Button upload;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

    }

    @Override
    public int intiLayout() {
        return R.layout.rfid_input;
//设置子类的布局
    }

    @Override
    public void initView() {//初始化控件
        num_lv= (LinearLayout) findViewById(R.id.num_lv);
        salvage_lv = (LinearLayout) findViewById(R.id.salvage_lv);
        year_lv = (LinearLayout) findViewById(R.id.year_lv);
        upload = (Button) findViewById(R.id.upload);
        num= (EditText) findViewById(R.id.num);
        remark= (EditText) findViewById(R.id.remark);
        salvage= (EditText) findViewById(R.id.salvage);
        year= (EditText) findViewById(R.id.year);
        assetNumbers = (EditText) findViewById(R.id.assetNumber);
        site_tv= (TextView) findViewById(R.id.site_tv);
        fh = (ImageView) findViewById(R.id.fh);
        dates = (TextView) findViewById(R.id.date);
        isdepre = (TextView) findViewById(R.id.isdepre);
        salvage_lv.setVisibility(View.GONE);
        year_lv.setVisibility(View.GONE);
    }

    @Override
    public void initData() {//初始化数据
        SharedPreferences mm = getSharedPreferences("mm", Context.MODE_PRIVATE);//1.通过上下文得到
        id = mm.getString("id","");
        upload.setOnClickListener(v -> new BottomMenuFragment(Rfid_Input.this)
                .addMenuItems(new MenuItem("上传数据"))
                .setOnItemClickListener((menu_item, position) -> {
                    if (position==0){
                        if (num.getText().toString().equals("")||site_tv.getText().toString().equals("")||dates.getText().toString().equals("")||isdepre.getText().toString().equals("")){
                            toastShort("请完善数据后重试！！！");
                            return;
                        }
                        if (isdepre.getText().toString().equals("是")){
                            if (!salvage.getText().toString().trim().equals("")&&!year.getText().toString().trim().equals("")){
                                uploads();
                            }else {

                                toastShort("净残值和使用年限不能为空");
                            }
                        }else if (isdepre.getText().toString().equals("否")){
                            uploads();
                        }

                    }
                })
                .show());
        assetId = getIntent().getStringExtra("assetId");
        assetNumber = getIntent().getStringExtra("assetNumber");
        istag = getIntent().getStringExtra("istag");
        fh.setOnClickListener(v -> finish());
        if (istag.equals("是")){
            assetNumbers.setText(assetNumber);
            assetNumbers.setFocusableInTouchMode(true);
            assetNumbers.setFocusable(true);
            assetNumbers.requestFocus();
            num_lv.setVisibility(View.GONE);
        }else if (istag.equals("否")){
            if (assetNumber.equals("")){
                assetNumbers.setText(assetNumber);
                assetNumbers.setFocusableInTouchMode(true);
                assetNumbers.setFocusable(true);
                assetNumbers.requestFocus();
            }else {
                assetNumbers.setText(assetNumber);
                assetNumbers.setFocusable(false);
                assetNumbers.setFocusableInTouchMode(false);
            }

        }

    }

    private void uploads() {
        String ster = "";
        if (isdepre.getText().toString().equals("是")){
            ster="1";
        }else if (isdepre.getText().toString().equals("否")){
            ster="0";
        }
        Map<String,String> map = new HashMap<>();
        map.put("id",id);
        map.put("source", Since_case.set().getSource());
        map.put("assetNumber",assetNumbers.getText().toString().trim());
        map.put("assetId",assetId);
        map.put("place",place);
        map.put("safedate",dates.getText().toString().trim());
        map.put("year",year.getText().toString());
        map.put("isdepre",ster);
        map.put("salvage",salvage.getText().toString());
        map.put("remark",remark.getText().toString());
        map.put("num",num.getText().toString());
        HttpOkHttp.getInstance().requestPost(url, map, new HttpOkHttp.OkHttpCallBack<String>() {
            @Override
            public void requestSuccess(final String s) {
                runOnUiThread(() -> {
                    JSONObject jsonObject = null;
                    try {
                        jsonObject = new JSONObject(s);
                        if ( jsonObject.getString("state").equals("1")){
                            toastShort("上传成功！");
                            assetNumbers.setText("");
                            finish();
                        }else {
                            toastShort("上传失败");
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                Log.i("123123123",s);
                });
            }

            @Override
            public void requestFailure(String message) {
               runOnUiThread(() -> Toast.makeText(Rfid_Input.this,"服务器error！！",Toast.LENGTH_SHORT).show());
            }
        },String.class);
    }


    @Override
    public void onDismiss(Object o) {

    }
    public void Date(View view){

DatePickDialog dialog = new DatePickDialog(Rfid_Input.this);
        //设置上下年分限制
        dialog.setYearLimt(20);
        //设置标题
        dialog.setTitle("选择时间");
        //设置类型
        dialog.setType(DateType.TYPE_YMD);
        //设置消息体的显示格式，日期格式
        dialog.setMessageFormat("yyyy-MM-dd");
        //设置选择回调
        dialog.setOnChangeLisener(null);
        //设置点击确定按钮回调
        dialog.setOnSureLisener(date -> {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String time = sdf.format(date);
            dates.setText(time);
        });
        dialog.show();

    }
    public void Isdepre(View view){
        new BottomMenuFragment(Rfid_Input.this)
                .addMenuItems(new MenuItem("是"))
                .addMenuItems(new MenuItem("否"))
                .setOnItemClickListener((menu_item, position) -> {
                    if (position==0){
                        isdepre.setText("是");
                        salvage_lv.setVisibility(View.VISIBLE);
                        year_lv.setVisibility(View.VISIBLE);
                    }
                    if (position==1){
                        isdepre.setText("否");
                        salvage_lv.setVisibility(View.GONE);
                        year_lv.setVisibility(View.GONE);
                    }
                })
                .show();
    }
    public  void Site(View view){
        Intent intent = new Intent(Rfid_Input.this, Shift_site.class);
        startActivityForResult(intent,3);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==3&&resultCode==3){
            site_tv.setText(data.getExtras().getString("site_name"));
            place = data.getExtras().getString("site_id");
        }
    }
}
